home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18001 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: Grouper.Exis.Net!usenet
  2. From: tlwrnce@exis.net (Tim Lawrence)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: BC 3.1 - 4.5 (error!?)
  5. Date: 18 Apr 1996 13:39:45 GMT
  6. Organization: Exchange Information Systems Networks
  7. Message-ID: <4l5gn1$gtb@Grouper.Exis.Net>
  8. References: <3173D628.48F6@demos.su>
  9. NNTP-Posting-Host: ppp-2-52.exis.net
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.93.11
  12.  
  13. Tested in BC 4.5 and 5.0.  Results where what I expected.  The reason you 
  14. may think you are getting unpredictable results is the use of a static var 
  15. in a function.
  16. In article <3173D628.48F6@demos.su>, 00alex@demos.su says...
  17. >
  18. >Hi, All
  19. >
  20. >Here is simple example of abnormal programming.
  21. >But BC crashed (sorry, it works and even compiles
  22. >the program, but it is unpredictable what will occur)
  23. >on it:
  24. >
  25. >#include <stdio.h>
  26. >
  27. >int st_func( int a )
  28. >{
  29. >  static int p=a;
  30.           ^^^  Stores a to p first time function used only!     
  31. >  return p++;
  32.     // Because p is static it will return p increment it and then
  33.     // use p+1 for p next time st_func is called
  34. >}
  35. >
  36. >int main( int argc, char *argv[] )
  37. >{
  38. >  int i;
  39. >  if( argc != 1 )
  40. >  {
  41. >    for( i = 10; i>0; i-- )
  42. >      printf( "%d\n", st_func( i ) );
  43. // First time st_func is called p will be set to 10 so output will be as 
  44. // follows:  10 11 12 13 14 15 16 17 18 19
  45. // NOTE: The parameter for st_func has meaning for the first time st_func
  46. //     is called ONLY! 
  47. >  }
  48. >  else
  49. >  {
  50. >    for( i = 0; i<10; i++ )
  51. >      printf( "%d\n", st_func( i ) );
  52. >  }
  53. >  return 0;
  54. >}
  55. >
  56. >Enjoy...
  57. >
  58. >If you know what is wrong reply please :)
  59.  
  60.